﻿using System;
using Base.Runtime;
using ResourceModule.Runtime;
using Timer.Runtime;
using UIModule;

namespace Logic
{
    public class GameManger : MonoBehaviourExt
    {
        public static GameManger Instance { get;private set;}
        public static UIWorld UI => Instance._ui;
        public static ResourcesLoaderAgent ResourcesLoader => Instance._resourcesLoader;
        public static Timer.Runtime.Timer Timer => Instance._timerManager.Timer;
        public static TimerManager TimerManager => Instance._timerManager;
        
        private UIWorld _ui;
        private ResourcesLoaderAgent _resourcesLoader;
        private TimerManager _timerManager;

        private void Awake()
        {
            Instance = this;
            InstanceComponents();
        }

        private void InstanceComponents()
        {
            _resourcesLoader = new ResourcesLoaderAgent();
            _timerManager = new TimerManager();
            _ui = new UIWorld();
        }

        private void Start()
        {
            Init();
        }

        private void Init()
        {
            _resourcesLoader.Init();
            _ui.Init();
            
            UI.Default.OpenWindow<TestWindow>("Prefabs/UI/TestWindow.prefab",null);
            UI.Default.OpenWindow<TestWindow>("Prefabs/UI/TestWindow1.prefab",null);
        }

        private void Update()
        {
            _timerManager.Update();
            _resourcesLoader.Update();
            _ui.Update();
        }

        private void Release()
        {
            _ui.Dispose();
            _timerManager.Dispose();
            _resourcesLoader.Dispose();
        }

        private void OnDestroy()
        {
            Release();
            Instance = null;
        }
    }
}